<template>
  <el-card class="box-card">
    <el-form
      ref="ruleForm"
      :model="ruleForm"
      label-width="120px"
      label-position="top"
      status-icon
      :rules="rules"
      class="demo-ruleForm"
    >
      <el-form-item label="姓名" >
        <el-input v-model="ruleForm.name" disabled></el-input>
      </el-form-item>
      <el-form-item label="账号">
        <el-input v-model="ruleForm.phone" disabled></el-input>
      </el-form-item>
      <el-form-item label="旧密码" prop="oldpassword">
        <el-input v-model="ruleForm.oldpassword" type="password" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newpassword">
        <el-input v-model="ruleForm.newpassword" type="password" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="请再次新密码" prop="newpassword1">
        <el-input v-model="ruleForm.newpassword1" type="password" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
    <el-button type="primary" @click="onSubmit">立即修改</el-button>
    <el-button>取消</el-button>
  </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
    } else if(value.length < 6) {
        callback(new Error("密码大于6位"));
    } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.newpassword) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
    return {
      ruleForm: {
        name: window.sessionStorage.getItem("name"),
        phone: window.sessionStorage.getItem("phone"),
        oldpassword: "",
        newpassword: "",
        newpassword1: ""
      },
      rules: {
        oldpassword: [{ validator: validatePass, trigger: "blur" }],
        newpassword: [{ validator: validatePass, trigger: "blur" }],
        newpassword1: [{ validator: validatePass2, trigger: "change" }]
      }
    };
  },
  methods: {
      onSubmit() {
          this.$axios.get(`/spider/user/changePassword?newPassword=${this.ruleForm.newpassword}&oldPassword=${this.ruleForm.oldpassword}&userId=${window.sessionStorage.getItem("id")}`).then(res => {
              if(res.data.messageModel.code == 0){
                  this.$message.success("密码修改成功")
              } else {
                  this.$message.console.error(res.data.messageModel.messageText);
                   this.ruleForm.oldpassword= "";
                   this.ruleForm.newpassword= "";
                   this.ruleForm.newpassword1= "";
              }
          })
      }
  }
};
</script>

<style>
.box-card {
  padding: 100px 300px;
}
</style>